package 简单.二叉树的层次遍历;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution1 {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> ans = new ArrayList<>();
            if(root==null) return ans;

            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                int size = queue.size();
                List<Integer> list = new ArrayList<>();
                for(int i = 0 ; i < size;i++){
                    TreeNode tmp = queue.poll();
                    int level = tmp.val;
                    list.add(level);
                    TreeNode left = tmp.left;
                    TreeNode right = tmp.right;
                    if(left!=null){
                        queue.offer(left);
                    }
                    if(right!=null){
                        queue.offer(right);
                    }
                }
                ans.add(0,list);
            }
            return ans;
        }
}
